package org.codefinger.dao;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import org.codefinger.dao.util.jdbc.JDBCUtil;

public class MapBuilder {

	private ResultSet			resultSet;

	private ResultSetMetaData	resultSetMetaData;

	private String[]			names;

	public MapBuilder(ResultSet resultSet, String[] names) throws SQLException {
		super();
		this.resultSet = resultSet;
		this.resultSetMetaData = resultSet.getMetaData();
		this.names = names;
	}

	public Map<String, Object> nextMap() throws SQLException {
		ResultSetMetaData resultSetMetaData = this.resultSetMetaData;
		String[] names = this.names;
		ResultSet resultSet = this.resultSet;
		Map<String, Object> map = new HashMap<String, Object>();
		int length = names.length;
		for (int i = 0; i < length;) {
			map.put(names[i++], JDBCUtil.getResult(//
					resultSet, i, resultSetMetaData.getColumnType(i)));
		}
		return map;
	}

}
